package com.xiaozhi.cloud.controller;


import com.xiaozhi.cloud.entites.PayDTO;
import com.xiaozhi.cloud.entities.Pay;
import com.xiaozhi.cloud.resp.ResultData;
import com.xiaozhi.cloud.service.PayService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
/*@Tag(tags = "支付微服务模块", description = "支付CRUD")*/
@Tag(name = "支付微服务模块", description = "支付CRUD")
@Slf4j
public class PayController {

    @Resource
    private PayService payService;

    @PostMapping(value = "/pay/add")
    @Operation(summary = "新增", description = "(新增支付流水方法,json串做参数)")
    public ResultData<String> addPay(@RequestBody Pay pay) {
        System.out.println(pay.toString());
        int i = payService.add(pay);
        return ResultData.success("成功插入记录，返回值：" + i);
    }

    @DeleteMapping(value = "/pay/del/{id}")
    @Operation(summary = "删除(删除支付流水方法)")
    // @ApiOperation(value = "删除(删除支付流水方法)")
    public ResultData<Integer> detelePay(@PathVariable("id") Integer id) {
        int i = payService.delete(id);
        return ResultData.success(i);
    }

    @PutMapping(value = "/pay/update")
    // @ApiOperation(value = "修改(修改支付流水方法)")
    private ResultData<String> updatePay(@RequestBody PayDTO payDTO) {
        Pay pay = new Pay();
        BeanUtils.copyProperties(payDTO, pay);
        int i = payService.update(pay);
        return ResultData.success("成功修改记录，返回值：" + i);
    }

    @GetMapping(value = "/pay/get/{id}")
//    @ApiImplicitParam(name = "id", value = "按照ID查流水(查询支付流水方法id)", required=true,
//            dataType = "Integer")
    public ResultData<Pay> getById(@PathVariable("id") Integer id) {
        Pay pay = payService.getById(id);
        PayDTO payDTO = new PayDTO();
        BeanUtils.copyProperties(pay,payDTO);
        //暂停62秒，测试出feign的默认超时时间
        try { TimeUnit.SECONDS.sleep(62); }catch (Exception e) {e.printStackTrace();}
        return ResultData.success(pay);
    }

    @GetMapping("/pay/all")
//    @ApiOperation(value = "查询支付的所有信息(查询支付的所有信息方法)")
    public ResultData<List<Pay>> getAll() {
        int i = 10 / 0;
        List<Pay> all = payService.getAll();
        return ResultData.success(all);
    }

//    @Value("${server.port}")
//    private String port;

    @GetMapping(value = "/pay/get/info")
    public String getInfoByConsul(){
        return "info: 8001";
    }
}
